package com.feng.HttpTools;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 
 * 下载文件
 * @author feng
 *
 */
public class DownLoad extends Thread {
	String urls;

	public DownLoad(String url) {
		this.urls = url;
	}
	
	public void downLoad(){
		
		try {
			URL url=new URL(urls);
			
			//设置为HttpURLConnection
			HttpURLConnection conn=(HttpURLConnection)url.openConnection();
			
			conn.setRequestMethod("GET");
			
			//设置apikey  默认为无
//			conn.setRequestProperty("apikey", "");
			
			conn.connect();//开启连接
			
			InputStream fis=conn.getInputStream();
			
			String format=urls.substring(urls.lastIndexOf("."));
			
			SimpleDateFormat sdf=new SimpleDateFormat("yyMMddHHmmss");
			
			FileOutputStream fos=new FileOutputStream(sdf.format(new Date())+format);
			
			byte b[]=new byte[1024];
			
			while(fis.read(b)!=-1){
				fos.write(b);
			}
			
			fos.flush();//将缓存区的数据全部写入
			
			fos.close();
			fis.close();
			
			
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	

}
